home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / COMNT109.ZIP / Comment.Cmd next >
Encoding:
Text File  |  1996-02-14  |  9.6 KB  |  282 lines

  1. /***************************************************************************
  2.  * Comment.Cmd 1.09 (14-Feb-1996 18:25:52)
  3.  *
  4.  * Copyright 1996 Christopher J. Madsen
  5.  *
  6.  * Comment.Cmd is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * Comment.Cmd is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * Christopher J. Madsen        <ac608@yfn.ysu.edu>
  21.  * 3222 Darby Ln.
  22.  * Denton, TX  76207-1305
  23.  *
  24.  * Display or change file comments
  25.  ***************************************************************************/
  26. '@echo off'
  27.  
  28. IF RxFuncQuery('SysLoadFuncs') THEN DO
  29.   CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  30.   CALL SysLoadFuncs
  31. END
  32.  
  33. PARSE SOURCE . . commandPath
  34. commandName = filespec('n',commandPath)
  35.  
  36. versionStr = 'Comment.Cmd version 1.09 (14-Feb-1996)'
  37.  
  38. /***************************************************************************
  39.  * Parse options:
  40.  ***************************************************************************/
  41. PARSE ARG commandLine
  42.  
  43. longOpts = '?:HELP: A:ALL-FILES: D:DELETE: F:FORCE: Q:QUIET: S:SUBJECT:'
  44. PARSE VALUE rxGetOpt(commandLine,'',longOpts) WITH resultCode commandLine
  45.  
  46. IF resultCode <> 0 THEN DO
  47.   SAY commandName': ' commandLine
  48.   SIGNAL Usage
  49. END
  50.  
  51. allFiles    = 0                 /* Do not list files without comments */
  52. deleting    = 0                 /* We are not deleting comments */
  53. eaHeader    = 'DFFF00000100FDFF'x
  54. eaDesc      = 'comments'        /* The EA name to use in messages */
  55. eaName      = '.comments'       /* The EA we are manipulating */
  56. forceUpdate = 0                 /* Don't replace existing comments */
  57. verbose     = 1                 /* Display progress messages */
  58.  
  59. globalVars = 'allFiles eaHdrLen eaHeader eaDesc eaName forceUpdate verbose'
  60.  
  61. DO FOREVER
  62.   PARSE VAR commandLine argument commandLine
  63.   SELECT
  64.     WHEN argument = '--' THEN LEAVE
  65.     WHEN argument = '-A' THEN
  66.       allFiles = 1              /* List all files */
  67.     WHEN argument = '-D' THEN
  68.       deleting = 1              /* Delete comments from files */
  69.     WHEN argument = '-F' THEN
  70.       forceUpdate = 1           /* Replace existing comments */
  71.     WHEN argument = '-Q' THEN
  72.       verbose = 0               /* Don't display progress messages */
  73.     WHEN argument = '-S' THEN DO
  74.       /* Work with the subject attribute */
  75.       eaHeader = 'FDFF'x
  76.       eaDesc = 'subjects'
  77.       eaName = '.subject'
  78.     END /* when --subject */
  79.     WHEN argument = '--LICENSE' THEN
  80.       SIGNAL License
  81.     WHEN argument = '--VERSION' THEN DO
  82.       SAY versionStr
  83.       EXIT
  84.     END /* when --version */
  85.     OTHERWISE
  86.       IF argument <> '-?' THEN
  87.         SAY commandName': Unrecognized option `'argument"'"
  88.       SIGNAL Usage
  89.   END /* select */
  90. END /* do while more options */
  91.  
  92. eaHdrLen = LENGTH(eaHeader)     /* Remember length of EA header */
  93.  
  94. /***************************************************************************
  95.  * Parse filenames and perform action:
  96.  ***************************************************************************/
  97. fileSpec = nextArg()
  98.  
  99. IF fileSpec = '' THEN SIGNAL Usage
  100.  
  101. IF deleting THEN DO
  102.   IF verbose THEN do
  103.     CALL CHAROUT ,,
  104.       'Are you sure you want to delete the' eaDesc 'from those files (Y/N)? '
  105.     key = SysGetKey()
  106.     SAY
  107.     IF key <> 'Y' & key <> 'y' THEN EXIT 1
  108.     SAY 'Deleting file' eaDesc'...'
  109.   END /* if verbose */
  110.   DO WHILE fileSpec <> ''
  111.     IF SysFileTree(fileSpec, 'file', 'O') THEN SIGNAL ERROR
  112.     CALL deleteComments
  113.     fileSpec = nextArg()
  114.   END /* DO */
  115. END /* if deleting */
  116. ELSE DO
  117.   comment = commandLine
  118.  
  119.   IF SysFileTree(fileSpec, 'file', 'O') THEN SIGNAL ERROR
  120.  
  121.   IF file.0 = 0 THEN DO
  122.     IF verbose THEN SAY 'No files.'
  123.     EXIT
  124.   END
  125.  
  126.   IF comment = '' THEN
  127.     CALL displayComments
  128.   ELSE
  129.     CALL setComments comment
  130. END /* else change or list comments */
  131.  
  132. EXIT /* main program */
  133.  
  134. /***************************************************************************
  135.  * Delete file comments
  136.  *
  137.  * The stem variable FILE should contain a list of complete pathnames.
  138.  * File.0 should contain the number of files.
  139.  ***************************************************************************/
  140. deleteComments: PROCEDURE EXPOSE file. (globalVars)
  141.   DO i = 1 TO file.0
  142.     fileName = filespec('n',file.i)
  143.     IF LENGTH(fileName) < 12 THEN fileName = RIGHT(fileName,12)
  144.     oldComment = readComment(file.i)
  145.     IF oldComment <> '' THEN DO
  146.       IF SysPutEA(file.i,eaName,'') = 0 & verbose THEN
  147.           SAY fileName': Deleted "'oldComment'"'
  148.       ELSE
  149.         SAY fileName': Error'
  150.     END /* if file has comment */
  151.   END /* for i */
  152.   RETURN
  153. /* end deleteComments */
  154.  
  155. /***************************************************************************
  156.  * Display file comments
  157.  *
  158.  * The stem variable FILE should contain a list of complete pathnames.
  159.  * File.0 should contain the number of files.
  160.  ***************************************************************************/
  161. displayComments: PROCEDURE EXPOSE file. (globalVars)
  162.   DO i = 1 TO file.0
  163.     comment = readComment(file.i)
  164.     IF (comment <> '') | allFiles THEN DO
  165.       fileName = filespec('n',file.i)
  166.       IF LENGTH(fileName) < 12 THEN
  167.         fileName = RIGHT(fileName,12)
  168.       SAY fileName':' comment
  169.     END /* if */
  170.   END /* for i */
  171.   RETURN
  172. /* end displayComments */
  173.  
  174. /***************************************************************************
  175.  * Set file comments
  176.  *
  177.  * The stem variable FILE should contain a list of complete pathnames.
  178.  * File.0 should contain the number of files.
  179.  ***************************************************************************/
  180. setComments: PROCEDURE EXPOSE file. (globalVars)
  181.   PARSE ARG comment
  182.  
  183.   fileComment = eaHeader||D2C(LENGTH(comment))||'00'x||comment
  184.   IF verbose THEN
  185.     SAY 'Setting file' eaDesc 'to "'comment'"...'
  186.   DO i = 1 TO file.0
  187.     fileName = filespec('n',file.i)
  188.     IF LENGTH(fileName) < 12 THEN fileName = RIGHT(fileName,12)
  189.     oldComment = readComment(file.i)
  190.     IF forceUpdate | oldComment = '' THEN DO
  191.       IF SysPutEA(file.i,eaName, fileComment) = 0 & verbose THEN DO
  192.         IF oldComment = '' THEN
  193.           SAY fileName': OK'
  194.         ELSE
  195.           SAY fileName': OK (Replaced "'oldComment'")'
  196.       END /* if verbose */
  197.       ELSE
  198.         SAY fileName': Error'
  199.     END /* if we should write comment to this file */
  200.     ELSE
  201.       IF verbose THEN SAY fileName': Did not replace "'oldComment'"'
  202.   END /* for i */
  203.   RETURN
  204. /* end setComments */
  205.  
  206. /***************************************************************************
  207.  * Read file comment
  208.  *
  209.  * Input:
  210.  *   pathname of file to get comment from
  211.  * Output:
  212.  *   The comment.  (Null string if no comment)
  213.  ***************************************************************************/
  214. readComment: PROCEDURE EXPOSE (globalVars)
  215.   PARSE ARG fileName
  216.   IF SysGetEA(fileName, eaName, 'comment') <> 0 THEN
  217.     RETURN ''
  218.   IF comment = '' THEN
  219.     RETURN ''
  220.   IF LEFT(comment,eaHdrLen) = eaHeader THEN
  221.     RETURN SUBSTR(comment,eaHdrLen+3)
  222.   ELSE
  223.     RETURN '<Multi-line comment>'
  224. /* end readComment */
  225.  
  226. /***************************************************************************
  227.  * Read an argument from the command line:
  228.  *
  229.  * Input:
  230.  *   The command line must be stored in commandLine
  231.  * Output:
  232.  *   Returns the argument.  If it was enclosed in quotation marks, the
  233.  *     quotes are removed.
  234.  *   The retrieved argument is removed from commandLine.
  235.  ***************************************************************************/
  236. nextArg: PROCEDURE EXPOSE commandLine
  237.   IF POS(LEFT(commandLine,1),'''"') > 0 THEN DO
  238.     /* Get a quoted argument */
  239.     quoteChar = LEFT(commandLine,1)
  240.     PARSE VAR commandLine (quoteChar) argument (quoteChar) commandLine
  241.   END /* if this is a quoted argument */
  242.   ELSE
  243.     PARSE VAR commandLine argument commandLine
  244.   commandLine = STRIP(commandLine,'L') /* Strip leading blanks */
  245.   RETURN argument
  246. /* end nextArg */
  247.  
  248. /***************************************************************************
  249.  * Signal Handlers
  250.  ***************************************************************************/
  251. Error:
  252.   SAY 'Abnormal exit!'
  253.   EXIT 1
  254.  
  255. License:
  256.   SAY
  257.   SAY versionStr
  258.   /* Read license text from top of command file */
  259.   CALL LINEIN commandPath, 1, 0
  260.   DO  3; CALL LINEIN commandPath;                     END
  261.   DO 19; CALL LINEOUT, SUBSTR(LINEIN(commandPath),4); END
  262.   EXIT
  263.  
  264. Usage:
  265.   SAY
  266.   SAY versionStr
  267.   SAY
  268.   SAY 'Usage: comment [options] fileSpec [comment]'
  269.   SAY '       comment -d fileSpec...'
  270.   SAY 'Options:'
  271.   SAY '    -a, --all-files List all files (even those without comments)'
  272.   SAY '    -d, --delete    Remove comments from specified files'
  273.   SAY '    -f, --force     Replace existing comments'
  274.   SAY '    -q, --quiet     Do not display progress messages',
  275.     'or ask for confirmation'
  276.   SAY '    -s, --subject   Work with the subject field',
  277.     'instead of the comments field'
  278.   SAY '    -?, --help      Display this help and exit'
  279.   SAY '        --license   Display license information and exit'
  280.   SAY '        --version   Display version information and exit'
  281.   EXIT
  282.